﻿using Mapster;
using Microsoft.EntityFrameworkCore;
using StackExchange.Redis;
using System.Numerics;
using WorldmeServer.Attributes;
using WorldmeServer.DTO.游戏相关;
using WorldmeServer.DTO.用户相关;
using WorldmeServer.Models.Base;
using WorldmeServer.Models.游戏相关;
using WorldmeServer.Services.Database;
using WorldmeServer.Utils;

namespace WorldmeServer.Services.Business.游戏相关
{
    [ScopedRegistration]
    public class PlayerService
    {
        private readonly MySqlContext mysql;
        public PlayerService(MySqlContext mysql)
        {
            this.mysql = mysql;
        }

        public async Task<ServiceResult<Player>> GetPlayer(string id)
        {
            var player = await mysql.Players.FirstOrDefaultAsync(p => p.Id == id);
            if (player == null)
            {
                return ServiceResult<Player>.Fail("角色不存在");
            }
            return ServiceResult<Player>.Success(player);
        }

        public async Task<ServiceResult<List<Player>>> GetPlayerList(string userId)
        {
            if (!await mysql.Users.AnyAsync(u => u.Id == userId))
            {
                return ServiceResult<List<Player>>.Fail("角色不存在");
            }
            var players = await mysql.Players.Where(x => x.UserId == userId).ToListAsync();
            return ServiceResult<List<Player>>.Success(players);
        }

        public async Task<ServiceResult<Player>> CreatePlayer(string userId, CreatePlayerDto dto)
        {
            if (await mysql.Players.AnyAsync(p => p.Name == dto.Name))
            {
                return ServiceResult<Player>.Fail("角色名已存在，请更换");
            }
            if (await mysql.Players.CountAsync(p => p.UserId == userId) == ConfigUtils.AppConfig.MaxPlayer)
            {
                return ServiceResult<Player>.Fail("角色数量已达上限");
            }
            Player player = dto.Adapt<Player>();
            player.Id = Guid.NewGuid().ToString("N");
            player.UserId = userId;
            await mysql.Players.AddAsync(player);
            await mysql.SaveChangesAsync();
            return ServiceResult<Player>.Success(player);
        }

        public async Task<ServiceResult<Boolean>> DeletePlayer(string userId, string id)
        {
            var player = await mysql.Players.FirstOrDefaultAsync(p => p.Id == id);
            if (player == null)
            {
                return ServiceResult<Boolean>.Fail("角色不存在");
            }
            if(player.UserId != userId)
            {
                return ServiceResult<Boolean>.Fail("无权限删除");
            }
            mysql.Players.Remove(player);
            await mysql.SaveChangesAsync();
            return ServiceResult<Boolean>.Success(true);
        }

        public async Task<ServiceResult<Player>> UpdatePlayer(string userId, UpdatePlayerDto dto)
        {
            var player = await mysql.Players.FirstOrDefaultAsync(p => p.Id == dto.Id);
            if (player == null)
            {
                return ServiceResult<Player>.Fail("角色不存在");
            }
            if (player.UserId != userId)
            {
                return ServiceResult<Player>.Fail("无权限修改");
            }
            player.Name = dto.Name;
            player.IsSlim = dto.IsSlim;
            mysql.Players.Update(player);
            await mysql.SaveChangesAsync();
            return ServiceResult<Player>.Success(player);
        }
    }
}
